package com.lisn.roomlib.FileInfo;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.lisn.commonlib.globals.AppGlobals;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : lishan
 * @e-mail : cnlishan@163.com
 * @date : 2020/8/12 4:04 PM
 * @desc :
 */
@Database(entities = {FileInfo.class}, version = 4)
//数据读取、存储时数据转换器,比如将写入时将Date转换成Long存储，读取时把Long转换Date返回
//@TypeConverters(DateConverter.class)
public abstract class FileInfoDatabase extends RoomDatabase {
    private static final FileInfoDatabase database;

    private static final int NUMBER_OF_THREADS = 4;
    public static final ExecutorService databaseWriteExecutor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);

    static {
        //数据库变动添加Migration
        Migration MIGRATION_1_2 = new Migration(1, 2) {
            @Override
            public void migrate(SupportSQLiteDatabase database) {
//                database.execSQL("ALTER TABLE FileInfo "
//                        + " ADD COLUMN age INTEGER NOT NULL DEFAULT 0");
                database.execSQL("ALTER TABLE FileInfo ADD COLUMN age INTEGER ");
            }
        };

        Migration MIGRATION_2_3 = new Migration(2, 3) {
            @Override
            public void migrate(SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE FileInfo ADD COLUMN age1 INTEGER DEFAULT 6");
            }
        };

        Migration MIGRATION_3_4 = new Migration(3, 4) {
            @Override
            public void migrate(SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE FileInfo ADD COLUMN age2 INTEGER DEFAULT 8");
            }
        };


        //创建一个内存数据库
        //但是这种数据库的数据只存在于内存中，也就是进程被杀之后，数据随之丢失
        //Room.inMemoryDatabaseBuilder()
        database = Room.databaseBuilder(AppGlobals.getApplication(),
                FileInfoDatabase.class, "jrzd_file_info.db")
                //是否允许在主线程进行查询
                .allowMainThreadQueries()
                //数据库创建和打开后的回调
                //.addCallback(sRoomDatabaseCallback)
                //设置查询的线程池
                //.setQueryExecutor()
                //.openHelperFactory()
                //room的日志模式
                //.setJournalMode()
                //数据库升级异常之后的回滚
                //.fallbackToDestructiveMigration()
                //数据库升级异常后根据指定版本进行回滚
                //.fallbackToDestructiveMigrationFrom()

                //添加数据库的变动迁移支持(当前状态从version1到version2的变动处理)
                //主要在user里面加入了age字段,大家可以git reset --hard <commit> 到第一个版本
                //然后debug 手动生成一些数据。然后debug 该版本查看数据库升级体现。
                .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
                .build();

    }


    public abstract FileInfoDao getFileInfoDao();

    public static FileInfoDatabase get() {
        return database;
    }


    private static final Callback sRoomDatabaseCallback = new Callback() {
        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            databaseWriteExecutor.execute(new Runnable() {
                @Override
                public void run() {

                    FileInfoDao dao = database.getFileInfoDao();
                    dao.deleteAll();

                }
            });
        }
    };


}
